import * as types from './mutation-types'
import {saveUserSimple, saveUserComplex, saveTokenInfo, saveLogin, cleanStorage} from 'database'
import {deepCopy} from 'common/func'

export const saveUser = function({commit}, user) {
  commit(types.SET_USER, saveUserSimple(user))
}

export const saveUserInfo = function({commit}, user) {
  // commit(types.SET_USER_INFO, saveUserComplex(user))
  commit(types.SET_USER_INFO, user)
}

export const saveToken = function({commit}, token) {
  commit(types.SET_TOKEN, saveTokenInfo(token))
}

export const clearLoginData = function({commit}) {
	commit(types.SET_LOGIN, false)
	commit(types.SET_TOKEN, new Object())
	commit(types.SET_USER, new Object())
	commit(types.SET_USER_INFO, new Object())
	cleanStorage('all')
}

export const modifyAvatar = function({commit, state}, src) {
  	let user = deepCopy(state.user)
  	user.avatar = src
  	let userInfo = deepCopy(state.userInfo)
  	userInfo.avatar = src
  	commit(types.SET_USER, saveUserSimple(user))
  	commit(types.SET_USER_INFO, saveUserComplex(userInfo))
}

export const saveUserList = function({commit}, list) {
	commit(types.SET_ONLINE, list)
}

export const saveChatting = function({commit, state}, item) {
  	let chatLog = deepCopy(state.chatLog)
    //更新正在聊天的列表
    addSnap(commit, state, item)
    if(!checkMsgLogExist(chatLog, item.unique)){
        chatLog[item.unique] = []
    }
    let nowChatList = chatLog[item.unique] 
	commit(types.SET_CHAT_PEOPLE, item)
	commit(types.SET_CHAT_LOG, chatLog)
	commit(types.SET_NOW_CHAT_LIST, nowChatList)
}

export const sendMessage = function({commit, state}, {unique, message}) {
	let chatLog = deepCopy(state.chatLog)
	let sendLog
	if(checkMsgLogExist(chatLog, unique)){
		sendLog = chatLog[unique]
		sendLog.push(message)
	}else{
		sendLog = [message]
	}
	chatLog[unique] = sendLog
	//将当前聊天人员移到首位
	moveChatItem(commit, state, unique)
	//提交
	commit(types.SET_CHAT_LOG, chatLog)
	commit(types.SET_NOW_CHAT_LIST, sendLog)
}

export const acceptMessage = function({commit, state}, message) {
	let send_unique = message.send
	let accept_unique = message.accept

	let chatLog = deepCopy(state.chatLog)
	let snapList = deepCopy(state.snap)

	let online = deepCopy(state.online)
	let index = findIndex(online,send_unique)
	let	user = online[index]

	let newMessage = new message({
        unique: user.unique,
        name: user.name,
        face: user.face,
        content: message.content,
        type: 'text'
    })
	if(accept_unique == 'allcomment') {
		send_unique = 'allcomment'
		user = deepCopy(state.groupObj)
	}
	
	//更新聊天页面
	addSnap(commit, state, user, true)

	let sendLog
	if(checkMsgLogExist(chatLog, send_unique)){
		sendLog = chatLog[send_unique]
		sendLog.push(newMessage)
	}else{
		sendLog = [newMessage]
	}
	chatLog[send_unique] = sendLog

	if(state.chatPeople.unique == send_unique) {
		commit(types.SET_NOW_CHAT_LIST, sendLog)
	}
	commit(types.SET_CHAT_LOG, chatLog)
}

export const newClient = function({commit, state}, client) {
	let online = deepCopy(state.online)
	let index = findIndex(online, client.unique)
	if(index !== false) {
		return false
	}
	online.push(client)
	commit(types.SET_ONLINE, online)
}

export const removeClient = function({commit, state}, client) {
	let online = deepCopy(state.online)
	let index = findIndex(online, client.unique)
	if(index === false) {
		return false
	}
	online.splice(index,1)
	commit(types.SET_ONLINE, online)
}

function addSnap(commit, state, user, type=false) {
  	let snapList = deepCopy(state.snap)
	let index = checkSnapExist(snapList, user.unique)
    if(index === false){
    	user.unRead = type ? 1 : 0
        snapList.unshift(user)
    }else{
    	if(type) {
        	snapList[index].unRead += 1
    	}else{
    		snapList[index].unRead = 0
    	}
    }
	commit(types.SET_SNAP, snapList)
}

function moveChatItem(commit, state, unique) {
	let snap = deepCopy(state.snap)
	if(snap.length < 2) {
		return false
	}
	let index = findIndex(snap, unique)
	if(index == 0) {
		return false
	}
	let item = snap[index]
	snap.splice(index,1)
	snap.unshift(item)
	commit(types.SET_SNAP, snap)
}

function checkSnapExist(array, unique){
    let isExist = false;
    for (let i = array.length - 1; i >= 0; i--) {
        if(array[i].unique === unique){
            isExist = i;
            break;
        }
    }
    return isExist;
}

function checkMsgLogExist(object, unique){
    return unique in object;
}

function findIndex(list, unique) {
  return list.findIndex((item) => {
    return item.unique === unique
  })
}